package com.ivo.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 
 * @author David Yang
 */
public class SpringUtil {
	private static Log log = LogFactory.getLog(SpringUtil.class);
	
	public static Object getBean(HttpServletRequest request, String beanName) {
		ApplicationContext appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		
		Object bean = null;
		try {
			bean = appCtx.getBean(beanName);
		} catch(Exception e) {
			log.info(e.getMessage());
		}
		return bean;
	}

	public static Object getBean(ServletContext context, String beanName) {
		ApplicationContext appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
		Object bean = null;
		try {
			bean = appCtx.getBean(beanName);
		} catch(Exception e) {
			log.info(e.getMessage());
		}
		return bean;
	}

}
